<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Illuminate\Support\Facades\DB;
use App\Helpers\SuccessResponse;
use App\Helpers\ValidatorResponse;
use App\Http\Constant;
use Ixudra\Curl\Facades\Curl;

class RegistController extends Controller{

	public function __construct(){}

	public function verifyCode(Request $request){
		$validator = Validator::make($request->all(), [
			'otp_id' => 'required|integer|min:1',
			'otp' => 'required|integer',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$otp_id = $request->input('otp_id');
			$otp = $request->input('otp');
			$otp_data = DB::connection('data_api_database')->select('select * from otps where id = '.$otp_id);
			$otp_data = json_decode(json_encode($otp_data), true);
			if(0 == count($otp_data)){
				return SuccessResponse::handler('This OTP code is not found.');
			}else{
				$otp_data = $otp_data[0];
				if($otp_data['expired_at'] < date('Y-m-d H:i:s', time())){
					$rtn = [
						'checked' => '0',
						'otp' => $otp_data['otp'],
						'message' => 'This OTP is expired.',
					];
				}else{
					if($otp == $otp_data['otp']){
						$rtn = [
							'checked' => 1,
							'otp' => $otp,
							'message' => 'This OTP is legal.',
						];
					}else{
						$rtn = [
							'checked' => 0,
							'otp' => $otp_data['otp'],
							'message' => 'This OTP is illegal.',
						];
					}
				}
				return SuccessResponse::handler($rtn);
			}
		}
	}
}
